﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using Microsoft.Win32;

namespace FetionLite
{
    public class FetionLiteSettings
    {
        private bool _saveAuth;
        private bool _autoLogin;
        private string _serverAddress;
        private string _userName;
        private string _password;
        private bool _enableSound;
        private bool _enableVibrate;

        private static readonly FetionLiteSettings _instance = new FetionLiteSettings();

        #region Constructors
        private FetionLiteSettings()
        {
            RegistryKey regKey = Registry.CurrentUser.CreateSubKey("software\\fetionlite\\");
            //RegistryKey regKey = Registry.CurrentUser.OpenSubKey("software\\fetionlite\\", true);

            string[] keys = regKey.GetValueNames();

            if (!StringExists(keys, "SaveAuth"))
                regKey.SetValue("SaveAuth", false);

            if (!StringExists(keys, "AutoLogin"))
                regKey.SetValue("AutoLogin", false);

            if (!StringExists(keys, "ServerAddress"))
                regKey.SetValue("ServerAddress", "");

            if (!StringExists(keys, "UserName"))
                regKey.SetValue("UserName", "");

            if (!StringExists(keys, "Password"))
                regKey.SetValue("Password", "");

            if (!StringExists(keys, "EnableSound"))
                regKey.SetValue("EnableSound", true);

            if (!StringExists(keys, "EnableVibrate"))
                regKey.SetValue("EnableVibrate", true);

            _saveAuth = bool.Parse(regKey.GetValue("SaveAuth").ToString());
            _autoLogin = bool.Parse(regKey.GetValue("AutoLogin").ToString());
            _serverAddress = regKey.GetValue("ServerAddress").ToString();
            _userName = regKey.GetValue("UserName").ToString();
            _password = regKey.GetValue("Password").ToString();
            _enableSound = bool.Parse(regKey.GetValue("EnableSound").ToString());
            _enableVibrate = bool.Parse(regKey.GetValue("EnableVibrate").ToString());

            regKey.Close();
        }
        #endregion

        private bool StringExists(string[] array, string str)
        {
            if (array == null)
                return false;
            else
            {
                foreach (string s in array)
                {
                    if (s.ToUpper() == str.ToUpper())
                        return true;
                }

                return false;
            }
        }

        public static FetionLiteSettings Instance
        {
            get
            {               
                return _instance;
            }
        }

        public void Persist()
        {
            RegistryKey regKey = Registry.CurrentUser.CreateSubKey("software\\fetionlite\\");
            
            regKey.SetValue("SaveAuth", _saveAuth);
            regKey.SetValue("AutoLogin", _autoLogin);
            regKey.SetValue("ServerAddress", _serverAddress);
            regKey.SetValue("UserName", _userName);
            regKey.SetValue("Password", _password);
            regKey.SetValue("EnableSound", _enableSound);
            regKey.SetValue("EnableVibrate", _enableVibrate);

            regKey.Close();
        }

        #region Properties
        public bool SaveAuth
        {
            get { return _saveAuth; }
            set { _saveAuth = value; }
        }
        public bool AutoLogin
        {
            get { return _autoLogin; }
            set { _autoLogin = value; }
        }
        public string ServerAddress
        {
            get { return _serverAddress; }
            set { _serverAddress = value; }
        }
        public string Mobile
        {
            get { return _userName; }
            set { _userName = value; }
        }
        public string Password
        {
            get { return _password; }
            set { _password = value; }
        }
        public bool EnableSound
        {
            get { return _enableSound; }
            set { _enableSound = value; }
        }
        public bool EnableVibrate
        {
            get { return _enableVibrate; }
            set { _enableVibrate = value; }
        }
        #endregion
    }    
}
